<template>
  <div>
    <parser :form-conf="formConf" @submit="submitForm" :key="key" ref="dynamicForm" v-if="isShowForm"/>
    <div slot="footer">
      <el-button >{{formConf.cancelButtonText||'取 消'}}</el-button>
      <el-button type="primary" @click="handelConfirm">{{formConf.confirmButtonText||'确 定'}}
      </el-button>
    </div>
  </div>
</template>

<script>
// import Parser from '@/parser/Parser'
import DevFormServiceApi from '@/service/dev/DevFormService'
export default {
  // components: { Parser },
  data() {
    return {
      key: +new Date(),
      formConf: {
       
      },
      isShowForm: false,
    
    }
  }, 
 async created(){
    await this.init()
  },
  mounted(){
  },
  methods: { 
    async init() {
      // console.log(this.$route)
      this.id = this.$route.query.id
      let params = {
        id: this.id,
      }
      let res = await DevFormServiceApi.queryById(params) 
      this.formConf = JSON.parse(res.data.formData)
      this.formConf.formModel="dataForm"
      // let result= await DevFormServiceApi.queryByIdFinancing({id: '015f8302c4c8035438f8f5d5cfdb505a'})
      // console.log(res)
      // console.log('formConfformConf :>> ', this.formConf);
      // // this.$refs.dynamicForm.formConf.formModel = this.formModel
      // this.fillFormData(this.formConf, result.data)
      this.isShowForm = true
    },
    handelConfirm() {
      this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
    },
    submitForm(data, callback) {
      console.log('submitForm提交数据：', data)
      console.log('this.$refs.formConf :>> ', this.$refs.dynamicForm);
      if (callback && typeof callback === "function") {
        callback()
      }
      this.submitClick(data) 
    },
    async submitClick(val) {
      console.log(val,'val')
      let res = await DevFormServiceApi.saveForm(val)
      console.log(res)
    },

    fillFormData(form, data) {
      const loop = (list, parent) => {
        for (let i = 0; i < list.length; i++) {
          let item = list[i]
          if (item.vModel) {
            const val = data.hasOwnProperty(item.vModel) ? data[item.vModel] : item.config.defaultValue
            if (!item.config.isSubTable) item.config.defaultValue = val
            if (!this.isPreview && this.useFormPermission) {
              let id = item.config.isSubTable ? parent.vModel + '-' + item.vModel : item.vModel
              let noShow = true
              if (this.formOperates && this.formOperates.length) {
                noShow = !this.formOperates.some(o => o.enCode === id)
              }
              noShow = item.config.noShow ? item.config.noShow : noShow
              this.$set(item.config, 'noShow', noShow)
            }
          }
          if (item.config && item.config.children && Array.isArray(item.config.children)) {
            loop(item.config.children, item)
          }
        }
      }
      loop(form.fields)
      form.formData = data
      console.info("-------form.formData------",form.formData);
    },
  }
}
</script>